package vn.edu.saigontech.ncms.web.student;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import vn.edu.saigontech.ncms.model.info.AbstractReceipt;
import vn.edu.saigontech.ncms.model.info.Student;
import vn.edu.saigontech.ncms.service.ProgramService;
import vn.edu.saigontech.ncms.service.StudentService;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class StudentReportAction extends BaseAction {
	private String fromDate;
	private String toDate;
	private int month;
	private long programId;

	public String showSearch() {
		REQUEST().setAttribute("programs", ProgramService.INSTANCE.findAllAreActive());
		return SUCCESS;
	}
	
	public String doSearchBirthDay() {
		Map<Student, List<AbstractReceipt>> map = StudentService.INSTANCE
				.findByBirthDay(this.month);
		List<Student> students = new ArrayList<Student>(map.keySet());
		Collections.sort(students, new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				Calendar cal1 = DateUtil.toCalendar(o1.getDateOfBirth());
				cal1.set(Calendar.YEAR, 2009);
				Calendar cal2 = DateUtil.toCalendar(o2.getDateOfBirth());
				cal2.set(Calendar.YEAR, 2009);
				return cal1.compareTo(cal2);
			}
		});
		REQUEST().setAttribute("map", map);
		REQUEST().setAttribute("students", students);
		
		this.addLoggingDetail(BaseAction.REPORT, "logging.birthday.report", this.month);
		return SUCCESS;
	}
	
	public String doSearchBirthDayByProgram() {
		Map<Student, List<AbstractReceipt>> map = StudentService.INSTANCE
				.findByBirthDayAndProgram(month, programId);
		List<Student> students = new ArrayList<Student>(map.keySet());
		Collections.sort(students, new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				Calendar cal1 = DateUtil.toCalendar(o1.getDateOfBirth());
				cal1.set(Calendar.YEAR, 2009);
				Calendar cal2 = DateUtil.toCalendar(o2.getDateOfBirth());
				cal2.set(Calendar.YEAR, 2009);
				return cal1.compareTo(cal2);
			}
		});
		REQUEST().setAttribute("map", map);
		REQUEST().setAttribute("students", students);
		
		this.addLoggingDetail(BaseAction.REPORT, "logging.birthday.report", this.month);
		return SUCCESS;
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public long getProgramId() {
		return programId;
	}

	public void setProgramId(long programId) {
		this.programId = programId;
	}
}